package com.example.controller.user;

import com.example.dto.business.UserBusinessDTO;
import com.example.response.ApiResult;
import com.example.vo.user.UserSearchVO;
import com.example.service.user.SearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Lexin Huang
 * 此控制器的所有方法, 不会进行参数的校验
 */
@Api(tags = "用户搜索模块")
@Validated
@RestController
public class SearchController {

    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @GetMapping("/users")
    @ApiOperation(value = "搜索相似昵称的用户")
    @ApiImplicitParam(value = "要搜索的用户名", dataTypeClass = String.class, paramType = "query")
    public ApiResult<UserSearchVO> searchUsersByUsername(@NotNull(message = "查询关键词不能为null!")
                                                         @Length(min = 1, message = "请至少输入一个关键字符!")
                                                         @RequestParam("keyword") String username){
        List<UserBusinessDTO> users = searchService.getUsersLike(username);
        UserSearchVO vo = new UserSearchVO();
        vo.setUsers(users);
        ApiResult<UserSearchVO> result = new ApiResult<>();
        result.setSuccess(vo);
        return result;
    }

}
